using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;

namespace DownloadTool
{
    public partial class NewDownloadDialog : Window
    {
        public string DownloadUrl { get; private set; } = string.Empty;

        public NewDownloadDialog()
        {
            InitializeComponent();
            UrlTextBox.Focus();
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            string url = UrlTextBox.Text.Trim();

            if (string.IsNullOrEmpty(url))
            {
                System.Windows.MessageBox.Show("请输入下载地址。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                UrlTextBox.Focus();
                return;
            }

            if (!Uri.TryCreate(url, UriKind.Absolute, out Uri? uriResult) || 
                (uriResult.Scheme != Uri.UriSchemeHttp && uriResult.Scheme != Uri.UriSchemeHttps))
            {
                System.Windows.MessageBox.Show("请输入有效的HTTP或HTTPS地址。", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                UrlTextBox.Focus();
                return;
            }

            DownloadUrl = url;
            DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
    }
}